<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <!-- 最新版本的 Bootstrap 核心 CSS 文件 -->
    <link rel="stylesheet" href="https://cdn.bootcss.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
    <link href="https://cdn.bootcss.com/select2/4.0.6-rc.1/css/select2.min.css" rel="stylesheet">
    <link rel="stylesheet" href="css/style.css">
    <title>北京实时公交查询</title>
</head>
<body>
    <h1 class='text-center'>北京公交实时查询</h1>
    <div class="container-fluid">
            <form id='busform'>
            <div class="form-group">
                <label for="bus-line">公交路线</label>
                <select name="bus-line" id="bus-line" class="chosen-select form-control" data-placeholder="请选择公交线路..." tabindex="1" >
                    <option value=""></option>
                    <?php
                    $buses = file_get_contents('bus.txt');
                    $buses = explode("\n",$buses);
                    foreach ($buses as $bus){
                        echo '<option value="'.$bus.'">'.$bus.'</option>';
                    }
                    ?>
                </select>
            </div>
            <div class="form-group">
                <label for="bus-direction">方向</label><mark class=log></mark>
                <select name="bus-direction" id="bus-direction" class="chosen-select form-control" data-placeholder="请选择公交线路..." tabindex="1"></select>
            </div>
            <div class="form-group">
                <label for="bus-stop">站点</label><mark class=log></mark>
                <select name="bus-stop" id="bus-stop" class="chosen-select form-control" data-placeholder="请选择公交站点..." tabindex="1"></select>
            </div>
            <button type="submit" class="btn btn-primary" id='update' disabled="disabled" >刷新</button>
        </form>
    
        <div class="panel panel-default" style="margin-top:10px;" id='result'>
            <div class="panel-body">
                <div id="bus-detail">
                </div>
            </div>
        </div>


    </div>
    <footer class="footer">
      <div class="container">
        <p class="text-muted text-center"><a href="https://fangd123.cn">fangd123</a>荣誉出品</p>
      </div>
    </footer>
<!-- 最新的 Bootstrap 核心 JavaScript 文件 -->
<script src="https://cdn.bootcss.com/jquery/3.3.1/jquery.min.js"></script>
<script src="https://cdn.bootcss.com/select2/4.0.6-rc.1/js/select2.min.js"></script>
<script>
    
    $(document).ready(function() {
        $('.chosen-select').select2({
            placeholder: '请选择...'
        });
        $( document ).ajaxStart(function() {
            $( ".log" ).text( "数据加载中...请稍等" );
        });
        $( document ).ajaxSuccess(function() {
            $( ".log" ).text( "加载完成...请选择" );
            $(".log").fadeOut('slow');
        });
        $('#result').hide();
    });

    function getBusDirec() {
    //获取后台json数据
        //console.log($("#bus-line").val());
        $.getJSON("server.php", {
            busline: $("#bus-line").val()
        }, function(data) {
            //console.log(data);
            var busdirection = $("#bus-direction");
            $("option", busdirection).remove(); //清空原有的选项 
            var blank_option = "<option value=''></option>";
            busdirection.append(blank_option);
            $.each(data, function(name, value) {
                //console.log(name);
                var option = "<option value='" + name + "'>" + value + "</option>";
                busdirection.append(option);
                //$("#bus-direction").trigger("chosen:updated");
            });
            busdirection.trigger('change');
        });
        
    }

    function getBusStop() {
    //获取后台json数据
        $.getJSON("server.php", {     
            busline: $("#bus-line").val(),
            busdirection: $("#bus-direction").val(),
        }, function(json) {
            var busstop = $("#bus-stop");
            $("option", busstop).remove(); //清空原有的选项 
            var blank_option = "<option value=''></option>";
            busstop.append(blank_option);
            $.each(json, function(name, value) {
                var option = "<option value='" + name + "'>" + value + "</option>";
                busstop.append(option);
            });
            $("#bus-stop").trigger("change");
        });
        
    }

    function getBusDetail() {
    //获取后台json数据
            console.log($("#bus-line").val())
            console.log($("#bus-direction").val())
            console.log($("#bus-stop").val())
        $.getJSON("server.php", {
            busline: $("#bus-line").val(),
            busdirection: $("#bus-direction").val(),
            busstop: $("#bus-stop").val(),
        }, function(data) {
            
            var busdetail = $("#bus-detail");
            busdetail.empty();
            busdetail.append('<p>'+data.bus_info+'</p');
            busdetail.append('<p>'+data.bus_coming+'</p');
            var stops = data.stops;
            $('#result').show();
            $('#update').removeAttr('disabled');
            // $.each(stops, function(name, value) {
            //     var option = "<option value='" + value + "'>" + name + "</option>";
            //     busdetail.append(option);
            // });
            //busdetail.trigger('change');
        });
    }


    $('#bus-line').on('select2:select', function (e) {
        getBusDirec();
    });

    $('#bus-direction').on('select2:select', function (e) {
        getBusStop();
    });

    $('#bus-stop').on('select2:select', function (e) {
        getBusDetail();
    });

    $("#busform").submit(function(event){
        // cancels the form submission
        event.preventDefault();
        getBusDetail();
    });
</script>
<script>
var _hmt = _hmt || [];
(function() {
  var hm = document.createElement("script");
  hm.src = "https://hm.baidu.com/hm.js?aeaf8324ef4a5d24e3397245e3761398";
  var s = document.getElementsByTagName("script")[0]; 
  s.parentNode.insertBefore(hm, s);
})();
</script>

</body>
</html>